package com.example.fuckyou;

import static com.example.fuckyou.MainActivity.kebiaodata;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class showkebiao extends AppCompatActivity {
    private FrameLayout popupOverlay;
    private List<Lesson> kebiaodata= new ArrayList<>();
    private static List<TextView> alltext=new ArrayList<>();
    private kebiaoManager km;
    public static Date firstSunday(Date startdate)
    {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(startdate);
        for (int i=0;i>=-6;i--)
        {
            calendar.add(Calendar.DATE,i);
            if (calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
            {
                break;
            }
        }
        return calendar.getTime();
    }
    public void changeweekclick(View view) throws ParseException {
        Button clickedButton = (Button) view;
        String buttonText = clickedButton.getText().toString();
        int weeknum=0;
        if (buttonText.length()==3)
        {
            weeknum=buttonText.charAt(1)-48;
        }
        else
        {
            weeknum=(buttonText.charAt(1)-48)*10+buttonText.charAt(2)-48;
        }
        Date weekdate=km.getweekdate(weeknum);
        displaylesson(weekdate);
        popupOverlay.setVisibility(View.GONE);
    }
    public void setdatetext(Date date)
    {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        SimpleDateFormat sdf=new SimpleDateFormat("M-d");
        List<TextView> datetext=new ArrayList<>();
        for (int i=115;i<=121;i++)
        {
            int resID = getResources().getIdentifier("textView" + i, "id", getPackageName());
            TextView textView = findViewById(resID);
            if (textView != null)
            {
                datetext.add(textView);
            }
        }
        for (int i=0;i<7;i++)
        {
            String textdate = sdf.format(date);
            datetext.get(i).setText(textdate);
            calendar.add(Calendar.DATE,1);
            date=calendar.getTime();
        }
    }
    public void displaylesson(Date date)
    {
        for (int i=23;i<=113;i++) {
            int resID = getResources().getIdentifier("textView" + i, "id", getPackageName());
            TextView textView = findViewById(resID);
            if (textView != null) {
                alltext.add(textView);
                textView.setText("");
                textView.setBackgroundColor(Color.WHITE);
            }
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-M-d");
        String weekdate=sdf.format(date);
        int weeknum=km.getweeknum(weekdate);
        Button bt1=findViewById(R.id.button3);
        bt1.setText("第"+weeknum+"週");
        setdatetext(date);
        try {
            kebiaodata=km.lessoninweek(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        for (Lesson test:kebiaodata)
        {
            int num=13*test.day+test.fromClass-1;
            TextView temp= alltext.get(num);
            String showtext=test.name+" ";
            if (!Objects.equals(test.classroom, "null"))
            {
                showtext+=test.classroom;
            }
            int groupSize = 10;
            List<String> parts = new ArrayList<>();
            for (int i = 0; i < showtext.length(); i += groupSize) {
                if (i+groupSize>showtext.length())
                {
                    parts.add(showtext.substring(i,showtext.length()));
                }
                else
                {
                    parts.add(showtext.substring(i, i + groupSize));
                }
            }
            temp.setText(parts.get(0));
            temp.setBackgroundColor(Color.parseColor("#FFBB33"));
            for(int i=1;i<=test.endClass-test.fromClass;i++)
            {
                TextView tt=alltext.get(num+i);
                if (i>=parts.size())
                {
                    tt.setText("");
                }
                else {
                    tt.setText(parts.get(i));
                }
                tt.setBackgroundColor(Color.parseColor("#FFBB33"));
            }
        }
    }
    public void weekclick(View view)
    {
        popupOverlay.setVisibility(TextView.VISIBLE);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        km=new kebiaoManager(this);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_showkebiao);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        popupOverlay = findViewById(R.id.frameLayout2);
        popupOverlay.setVisibility(TextView.GONE);
        popupOverlay.setOnClickListener(v -> popupOverlay.setVisibility(View.GONE));
        Date date=new Date();
        Date thissunday=firstSunday(date);
        displaylesson(thissunday);
    }
}